package cn.john.hrm.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

// 交给Spring管理
@Component
public class FeignResourceInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 根据请求持有对象获取请求相关的属性
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
        // 获取当前请求
        HttpServletRequest currentReq = requestAttributes.getRequest();
        // 获取请求头中token
        String authorization = currentReq.getHeader("Authorization");
        // 将原来请求中的token放入feign发起的请求中
        requestTemplate.header("Authorization",authorization);
    }
}
